package com.tomjun.permission.controller;

import java.util.Map;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.tomjun.common.JsonModel;
import com.tomjun.permission.model.RoleInfo;
import com.tomjun.permission.service.RoleService;

@RestController
@RequestMapping("/role")
public class RoleController {
	@Autowired
	private RoleService roleService;
	/**
	 * 查看角色关联的所有信息，用户以及权限
	 * @param page
	 * @param pageSize
	 * @param map
	 * @return
	 */
	@RequiresPermissions("role:list")
	@RequestMapping(value = "/{page}/{pageSize}",method = RequestMethod.GET)
	public JsonModel<Object> getAllRole(@PathVariable("page") int page,@PathVariable("pageSize") int pageSize,
			@RequestParam Map<String, Object> map){
		JsonModel<Object> allRoleModel=new JsonModel<Object>();
		try {
			return roleService.getAllRole(page, pageSize, map);
		} catch (Exception e) {
			// TODO: handle exception
			System.err.println(e);
			allRoleModel.setCode(1);
			allRoleModel.setMsg("fail");
		}
		return allRoleModel;
	}
	
	/**
	 * 获取所有的模块的权限信息
	 * @return
	 */
	@RequiresPermissions(value = {"role:add", "role:update"}, logical = Logical.OR)
	@RequestMapping(value = "/allPermission",method = RequestMethod.GET)
	public JsonModel<Object> getAllPermission(){
		JsonModel<Object> allRoleModel=new JsonModel<Object>();
		try {
			return roleService.getAllPermission();
		} catch (Exception e) {
			// TODO: handle exception
			System.err.println(e);
			allRoleModel.setCode(1);
			allRoleModel.setMsg("fail");
		}
		return allRoleModel;
	}
	
	/**
	 * 新增角色以及角色权限信息
	 * @param roleInfo
	 * @return
	 */
	@RequiresPermissions("role:add")
	@RequestMapping(value = "/addRole",method = RequestMethod.POST)
	public JsonModel<String> addRole(@RequestBody RoleInfo roleInfo){
		JsonModel<String> jsonModel=new JsonModel<String>();
		try {
			if(roleService.existRole(roleInfo)) {
				jsonModel.setCode(1);
				jsonModel.setMsg("改角色名已经存在");
				return jsonModel;
			}
			roleService.addRole(roleInfo);
			return jsonModel.generateSuccessObject(null); 
		} catch (Exception e) {
			// TODO: handle exception
			return jsonModel.generateFailue("fail");
		}
	}
	/**
	 * 更新角色以及角色权限信息
	 * @param roleInfo
	 * @return
	 */
	@RequiresPermissions("role:update")
	@RequestMapping(value = "/updateRole",method = RequestMethod.PUT)
	public JsonModel<String> updateRole(@RequestBody RoleInfo roleInfo){
		JsonModel<String> jsonModel=new JsonModel<String>();
		try {
			if(roleService.existRole(roleInfo)) {
				jsonModel.setCode(1);
				jsonModel.setMsg("改角色名已经存在");
				return jsonModel;
			}
			roleService.updateRole(roleInfo);
			return jsonModel.generateSuccessObject(null); 
		} catch (Exception e) {
			// TODO: handle exception
			return jsonModel.generateFailue("fail");
		}
	}
	
	/**
	 * 删除角色以及该角色的权限信息
	 * @param roleId
	 * @return
	 */
	@RequiresPermissions("role:delete")
	@RequestMapping(value = "/{roleId}",method = RequestMethod.DELETE)
	 public JsonModel<String> deleteRole(@PathVariable("roleId") Integer roleId){
		JsonModel<String> jsonModel=new JsonModel<String>();
		try {
			roleService.deleteRoleById(roleId);
			return jsonModel.generateSuccessObject(null); 
		} catch (Exception e) {
			// TODO: handle exception
			return jsonModel.generateFailue("fail");
		}
	}
}
